package com.kuqi.controller;

import com.kuqi.domain.Books;
import com.kuqi.service.BookService;
import com.kuqi.service.impl.BookServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author:kuqi
 * @createTime:2021/10/22 21:40
 * @description:
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    //查询全部书籍，并返回到一个书籍展示页面
    @RequestMapping("/allBook.do")
    public ModelAndView list(){
        List<Books> books = bookService.queryAllBook();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("books",books);
        modelAndView.setViewName("allBook");
        System.out.println("books:"+books);
        return modelAndView;
    }

    //跳转到添加书籍页面
    @RequestMapping("/toAddBook.do")
    public String toAddPages(){
        return "addBook";
    }


    //增加书籍业务
    @RequestMapping("/addBook.do")
    public String addBook(Books books){
        System.out.println("book+"+books);
        bookService.addBook(books);
        return "redirect:/book/allBook.do";//重定向
    }

    //跳转到修改页面
    @RequestMapping("/toUpdate.do")
    public String toUpdatePages(Integer id, Model model){
        Books books = bookService.queryBooksById(id);
        model.addAttribute(books);
        return "updateBook";
    }

    //修改书籍信息业务
    @RequestMapping("/update.do")
    public String update(Books books){
        System.out.println("books==>"+books);
        bookService.updateBook(books);
        return "redirect:/book/allBook.do";
    }


    //删除书籍
    @RequestMapping("/delete.do/{bookId}")
    public String delete(@PathVariable("bookId") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook.do";
    }
}
